var UCenterCommon = (function ($){
	//var countdown = 60;
	var item;
	return {
		countdown : 60,
		init : function () {
			
			var me = this;
			
			var weekPasswordInfo = $('#weekPasswordInfo').val();
			
			if (weekPasswordInfo != '') {
				$('#errorInfo').html('<div class="error-ts error-img">' + weekPasswordInfo + '</div>');
			}
			
			$('#userAccount').bind('input propertychange blur', function() {
				me.validUserAccount();
			});
			
			$('#oldPassword').bind('input propertychange blur', function() {
				me.validOldPassword();
			});
			
			$('#newPassword').bind('input propertychange blur', function() {
				me.validNewPassword();
				me.validCheckPassword();
			});
			
			$('#checkPassword').bind('input propertychange blur', function() {
				me.validCheckPassword();
			});
			
		},

		changePassword : function () {
			
			var me = this;
			
			var userAccount = $("#userAccount").val();
			var oldPassword = $("#oldPassword").val();
			var newPassword = $("#newPassword").val();
			var checkPassword = $("#checkPassword").val();
			
			if (!me.validUserAccount()) {
				return;
			}
			
			if (!me.validOldPassword()) {
				return;
			}
			
			if (!me.validNewPassword()) {
				return;
			}
			
			if (!me.validCheckPassword()) {  
	            return;
	        }
			
			jQuery.ajax({
				type : "POST",
				url : BASE_PATH + 'changePassword',
				data : {
					userAccount		: userAccount,
					oldPassword		: oldPassword,
					newPassword		: newPassword
				},
				async : false,
				//返回值
				success : function(data) {
					if (!data.success) {
						var errorInfo = '<div class="error-ts error-img">' + data.message + '</div>';
						
						$('#errorInfo').html(errorInfo);
						return;
					}
					var redirectUri = $("#redirectUri").val();
					window.location.href = decodeURIComponent(decodeURIComponent(redirectUri));
					
				}
			});

		},
		
		validNewPassword : function () {
			
			var me = this;
			
			var newPassword = $("#newPassword").val();
			var ls = me.isSimplePwd(newPassword);
			
			if (ls < 2) {  
	            $('#newPasswordTip').html("弱：新密码长度必须不小于8位且包含数字、小写字母、大写字母、特殊字符中的两项！！")
	            $('#mmqrR').attr('class', 'mmqr-r');
	            $('#mmqrZ').attr('class', 'mmqr-z mmqr-normal');
	            $('#mmqrQ').attr('class', 'mmqr-q mmqr-normal');
	            return false;
	        } else if (ls == 2) {
	        	$('#newPasswordTip').html("中")
	        	$('#mmqrR').attr('class', 'mmqr-r');
	            $('#mmqrZ').attr('class', 'mmqr-z');
	            $('#mmqrQ').attr('class', 'mmqr-q mmqr-normal');
	            return true;
	        } else if (ls > 2) {
	        	$('#newPasswordTip').html("强")
	        	$('#mmqrR').attr('class', 'mmqr-r');
	            $('#mmqrZ').attr('class', 'mmqr-z');
	            $('#mmqrQ').attr('class', 'mmqr-q');
	            return true;
	        }
			
		},
		
		validCheckPassword : function () {
			
			var me = this;
			
			var newPassword = $('#newPassword').val();
			var checkPassword = $('#checkPassword').val();
			
			if (checkPassword != newPassword) {
				$('#checkPassword').parents('.form-group').addClass('has-error has-feedback');
				$('#checkPassword').parent().find('.glyphicon-remove').remove();
				$('#checkPassword').parent().append('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true" style="line-height:42px;">');
				return false;
			} else {
				$('#checkPassword').parents('.form-group').removeClass('has-error has-feedback');
				$('#checkPassword').parent().find('.glyphicon-remove').remove();
				return true;
			}
			
		},
		
		validUserAccount : function () {
			
			var userAccount = $("#userAccount").val();
			
			if (userAccount == '') {
				$('#userAccount').parents('.form-group').addClass('has-error has-feedback');
				$('#userAccount').parent().find('.glyphicon-remove').remove();
				$('#userAccount').parent().append('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true" style="line-height:42px;">');
				return false;
			} else {
				$('#userAccount').parents('.form-group').removeClass('has-error has-feedback');
				$('#userAccount').parent().find('.glyphicon-remove').remove();
				return true;
			}
			
		},
		
		validOldPassword : function () {
			
			var oldPassword = $("#oldPassword").val();
			
			if (oldPassword == '') {
				$('#oldPassword').parents('.form-group').addClass('has-error has-feedback');
				$('#oldPassword').parent().find('.glyphicon-remove').remove();
				$('#oldPassword').parent().append('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true" style="line-height:42px;">');
				return false;
			} else {
				$('#oldPassword').parents('.form-group').removeClass('has-error has-feedback');
				$('#oldPassword').parent().find('.glyphicon-remove').remove();
				return true;
			}
			
		},
		
		isSimplePwd : function (password) {
	        if(password.length < 6) {  
	            return 0;  
	        }  
	        var ls = 0;  
	        if (password.match(/([a-z])+/)) {  
	            ls++;  
	        }  
	        if (password.match(/([0-9])+/)) {  
	            ls++;  
	        }  
	        if (password.match(/([A-Z])+/)) {  
	            ls++;  
	        }  
	        if (password.match(/[^a-zA-Z0-9]+/)) {  
	            ls++;  
	        }  
	        return ls;  
	    },
	    
	    getWedoUserInfo : function () {
	    	
			var returnParams = $('#returnParams').val();
			returnParams = decodeURIComponent(decodeURIComponent(returnParams));
			var callUrl = $('#callUrl').val();
			callUrl = decodeURIComponent(decodeURIComponent(callUrl));
			$('#callUrl').val(callUrl);
			
			$('#returnParams').val(returnParams);
			
			if (!$('#client_id').val()) {
				$('#client_id').val('XDW_OA');
			}
			
			if (!$('#client_secret').val()) {
				$('#client_secret').val('XDW_OA');
			}
			
			$('#response_type').val('code');
			$('#scope').val('read');
			$('#redirect_uri').val('url');
			$('#state').val('1');
			$('#oaRedirectForm').submit();

	    },
	    
	    /**
	     * 微信账号登陆
	     */
	    getWechatUserInfo : function () {
	    	
			if ($('#code').val() && $('#state').val() == '') {
				$('#client_secret').val('temp');
				$('#response_type').val('code');
				$('#scope').val('read');
				$('#state').val('1');
				$('#wechatRedirectForm').submit();
			}
	    },
	    
	    openUpload : function () {
	    	window.location.href = BASE_PATH + 'resources/AvatarUpload/index.html?portraitName=' + ($('#headPortrait').attr('portraitName') || '');
	    },
	    
	    /**
	     * 获取用户信息
	     */
	    getUserInfo : function () {
	    	
	    	var me = this;
	    	
	    	jQuery.ajax({
				type : "POST",
				url : BASE_PATH + 'rs/userinfo',
				async : false,
				//返回值
				success : function(data) {
					$('#username').val(data['username']);
					$('#nickname').val(data['nickname']);
					$('input[type="radio"][name="sex"][value="' + data['sex'] + '"]').prop("checked", true);
					$('#email').val(data['email']);
					$('#mobilePhone').val(data['mobilePhone']);
					$("#mobilePhone").attr("disabled",true);
					$('#Phone').val(data['mobilePhone']);
					if(data['mobilePhone'] == "" || data['mobilePhone'] == null){
						$("#bound").show();
						$("#rebound").hide();
						$("#Phone").attr("disabled",false);
					}
					else{
						$("#bound").hide();
						$("#rebound").show();
						$("#Phone").attr("disabled",true);
					}
					if (data['headPortrait']) {
						$('#headPortrait').attr('src', data['headPortrait']);
						$('#headPortrait').attr('portraitName', data['headPortrait'].split('/').pop().split('.')[0]);
					}
					if(data['shield'] != undefined){
						var shield = data['shield'][0].split(",");
						if(shield.lentgh !=0) {
							for(var i=0;i<shield.length;i++) {
								var id=shield[i];
								if(id =='' || id == null) {
									continue;
								}
								id = id+'Div';
								var c = document.getElementById(id);
								if(c!=null) {
									$("#"+id).remove();
								}
							}
						}
					}
				},
				
				statusCode: {
				    401 : function() {
				    	window.location.href = BASE_PATH + 'WEB-INF/jsp/401.jsp';
				    },
				    404 : function() {
				    	window.location.href = BASE_PATH + 'WEB-INF/jsp/404.jsp';
					},
					500 : function() {
						window.location.href = BASE_PATH + 'WEB-INF/jsp/500.jsp';
					}
				}
			});
	    	
	    	$('#nickname').bind('input propertychange blur', function() {
				me.validNickname();
			});
	    	
	    	$('#email').bind('input propertychange blur', function() {
				me.validEmail();
			});
	    	
	    	$('#Phone').bind('input propertychange blur', function() {
	    		me.validMobilePhone();
	    	});
	    	
	    },
	    
	    /**
	     * 保存用户信息
	     */
	    updateUserInfo : function () {
	    	
	    	var me = this;
	    	
	    	if (!me.validNickname()) {
				return;
			}
			
			if (!me.validEmail()) {
				return;
			}
			
			if (!me.validMobilePhone()) {
				return;
			}
	    	
	    	var userInfoObj = {
	    			nickname 	: $('#nickname').val() || '',
	    			sex 		: $('[name=sex]:checked').val() || '',
	    			email 		: $('#email').val() || ''
	    	}
	    	
	    	jQuery.ajax({
				type : "POST",
				url : BASE_PATH + 'rs/updateUserInfo',
				async : false,
				data : {
					json : JSON3.stringify(userInfoObj)
				},
				//返回值
				success : function(data) {
					
					alert(data['message']);
					
				},
				
				statusCode: {
				    401 : function() {
				    	window.location.href = BASE_PATH + 'WEB-INF/jsp/401.jsp';
				    },
				    404 : function() {
				    	window.location.href = BASE_PATH + 'WEB-INF/jsp/404.jsp';
					},
					500 : function() {
						window.location.href = BASE_PATH + 'WEB-INF/jsp/500.jsp';
					}
				}
			});
	    },
	    
	    /**
	     * 验证昵称
	     */
	    validNickname : function () {
			
			var nickname = $("#nickname").val();
			
			if (nickname == '') {
				$('#nickname').parents('.form-group').addClass('has-error has-feedback');
				$('#nickname').parent().find('.glyphicon-remove').remove();
				$('#nickname').parent().append('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true" style="line-height:42px;">');
				return false;
			} else {
				$('#nickname').parents('.form-group').removeClass('has-error has-feedback');
				$('#nickname').parent().find('.glyphicon-remove').remove();
				return true;
			}
			
		},
		
		validEmail : function () {
			
			var email = $("#email").val();
			
			if (email == '') {
				$('#email').parents('.form-group').addClass('has-error has-feedback');
				$('#email').parent().find('.glyphicon-remove').remove();
				$('#email').parent().append('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true" style="line-height:42px;">');
				$('#emailTip').html('请输入正确的邮箱地址！！');
				return false;
			} else {
				
				var myreg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
				
				if(!myreg.test(email)) {
					$('#email').parents('.form-group').addClass('has-error has-feedback');
					$('#email').parent().find('.glyphicon-remove').remove();
					$('#email').parent().append('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true" style="line-height:42px;">');
					$('#emailTip').html('请输入正确的邮箱地址！！');
					return false;
				}
				
				$('#email').parents('.form-group').removeClass('has-error has-feedback');
				$('#email').parent().find('.glyphicon-remove').remove();
				$('#emailTip').html('');
				return true;
			}
			
		},
		
		/**
		 * 验证手机号
		 */
		validMobilePhone : function () {
			
			var mobilePhone = $("#mobilePhone").val();
			var Phone = $("#Phone").val();
			
			if (Phone == '') {
				$('#mobilePhoneTip').html('');
				return true;
			}else{
				
				var myreg = /^$|(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;
				
				if(!myreg.test(Phone)) {
					$('#message').html('请输入正确的手机号！');
					return false;
				}
				
				$('#message').html('');
				return true;
			}
			
		},
		
		loginSubmit : function () {
			$('#loginForm').find('[name=redirect_uri]').val(decodeURIComponent($('#loginForm').find('[name=redirect_uri]').val()));
//			$("#password").val(DES3.encrypt("ZGJMWQJSTMDZQQRDOONE" + (new Date()).getFullYear(), $("#password").val()));
			document.getElementById('loginForm').submit();
		},
		
		bindingSubmit : function () {
			$('#loginForm').find('[name=redirect_uri]').val(decodeURIComponent($('#loginForm').find('[name=redirect_uri]').val()));
//			$("#password").val(DES3.encrypt("ZGJMWQJSTMDZQQRDOONE" + (new Date()).getFullYear(), $("#password").val()))
			document.getElementById('bindingForm').submit();
		},
		
		boundPhone : function () {
			var data = {
					userAccount : $('#username').val() || null,
					mobilePhone : $('#Phone').val() || null,
					code: $('#code').val()
			}
			jQuery.ajax({
				type : "POST",
				url : BASE_PATH + "rs/modifyMobilePhone.action",
				data:data,
				async : false,
				//返回值
				success : function(res) {
					if(res.success == true){
						var phone = $("#Phone").val();
						$("#popup-send").text("绑定成功");
						$("#popup-send").fadeIn(100).delay(800).fadeOut(100,function(){
							$("#Phone").attr("disabled",true);
							$("#mobilePhone").attr("disabled",true);
							$("#Phone").val(phone);
							$("#mobilePhone").val(phone);
							$("#bound").hide();
							$("#rebound").show();
							$("#myModal").modal("hide");
						});
					}else{
						$("#message").text(res.message);
					}
				}
			});
		},
		
		reboundPhone : function () {
			var data = {
					userAccount : $('#username').val() || null,
					mobilePhone : $('#Phone').val() || null,
					code: $('#code').val()
			}
			jQuery.ajax({
				type : "POST",
				url : BASE_PATH + "rs/mobilePhoneUnbound.action",
				data:data,
				async : false,
				//返回值
				success : function(res) {
					if(res.success == true){
						$("#popup-send").text("解绑成功");
						$("#popup-send").fadeIn(100).delay(800).fadeOut(100,function(){
							$("#mobilePhone").val("");
							$("#Phone").val("");
							$("#Phone").attr("disabled",false);
							$("#bound").show();
							$("#rebound").hide();
							$("#myModal").modal("hide");
						});
					}else{
						$("#message").text(res.message);
					}
				}
			});
		},
		
		sendPhoneCode : function (code) {
			$(code).removeAttr("onclick");
			var mobilePhone = $("#Phone").val();
			var me = this;
			$("#message").text("");
			if( $.trim(mobilePhone)=="" ){
				var errorMsg = '手机号不能为空!';
				$("#message").text(errorMsg);
		     	$('#Phone').focus();
		     	$(code).attr("onclick","javascript:UCenterCommon.sendPhoneCode(this);void(0);");
		     	return;
		    }
			if( mobilePhone !="" && !/1(3[0-9]|5[0-35-9]|8[0235-9])[0-9]{8}$/.test(mobilePhone)){
				var errorMsg = '请输入正确的手机号！';
				$("#message").text(errorMsg);
		     	$('#Phone').focus();
		     	$(code).attr("onclick","javascript:UCenterCommon.sendPhoneCode(this);void(0);");
		     	return;
		   	}
								
			var data = {
					mobilePhone : $('#Phone').val() || null,
					userAccount : $('#username').val() || null
			}
			jQuery.ajax({
				type : "POST",
				url : BASE_PATH + "rs/sendSecurityCode.action",
				data:data,
				async : false,
				//返回值
				success : function(res) {
					if(res.success == true){
						if(res.message == "发送成功"){
							UCenterCommon.settime(code);
						}
						else{
							$("#message").text(res.message);
						}
					}else{
						$(code).attr("onclick","javascript:UCenterCommon.sendPhoneCode(this);void(0);");
						$("#message").text(res.message);
					}
				}
			});
		},
		
		settime : function (obj) {
			var me = this;
			if (me.countdown == 0) {
				$(obj).css("color","#428BCA");
				$(obj).attr("onclick","javascript:UCenterCommon.sendPhoneCode(this);void(0);");
				$(obj).text("点击获取");
				me.countdown = 60;  
		    } else {
		    	$(obj).css("color","#aaa");
				$(obj).text(me.countdown + "s后重发");
				me.countdown--;  
		        setTimeout(function() {  
		        	UCenterCommon.settime(obj);  
		        },  
		        1000)
		    }
		}
		
		

	}
})(jQuery);